package com.lemi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lemi.dao.FeedbackMapper;
import com.lemi.entity.Feedback;
import com.lemi.entity.User;
import com.lemi.util.BspEntity;
import com.lemi.util.BspResult;
import com.lemi.util.BspUtils;
import com.lemi.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("feedback")
public class FeedbackController {

    @Resource
    private FeedbackMapper feedbackMapper;

    @RequestMapping("list")
    public String list(Model model, BspEntity bspEntity){
        model.addAttribute("bspEntity",bspEntity);
        return "problem/feedback";
    }

    @RequestMapping("listData")
    @ResponseBody
    public Object listData(BspEntity bspEntity){
        try {
            Example fe = new Example(Feedback.class);
            Example.Criteria criteria = fe.createCriteria();
            criteria.andEqualTo("delFlag",false);

            if(StringUtils.isNotBlank(bspEntity.getName())){
                criteria.andLike("feedbackName","%"+bspEntity.getName()+"%");
            }

            if(StringUtils.isNotBlank(bspEntity.getManagerName())){
                criteria.andLike("replyName","%"+bspEntity.getManagerName()+"%");
            }

            if(bspEntity.getStartDate()!=null){
                criteria.andGreaterThanOrEqualTo("createTime",bspEntity.getStartDate());
            }

            if(bspEntity.getEndDate()!=null){
                criteria.andLessThan("createTime", BspUtils.getDayTime(bspEntity.getEndDate(),1));
            }


            PageHelper.startPage(bspEntity.getPage(),bspEntity.getLimit());
            PageHelper.orderBy(bspEntity.getOrderColumn()+" "+bspEntity.getOrderDir());

            List<Feedback> feedbacks = feedbackMapper.selectByExample(fe);
            PageInfo<Feedback> orderPageInfo = new PageInfo<>(feedbacks);
            bspEntity.setTotal(orderPageInfo.getTotal());
            bspEntity.setListData(feedbacks);
            return BspResult.ok("",bspEntity);
        }catch (Exception e){
            e.printStackTrace();
            return BspResult.err(Constant.SERVER_ERR);
        }
    }
}
